#ifndef __mytime__
#define __mytime__


#include <NTPClient.h>

WiFiUDP ntpUDP;                                                           // 互联网时钟
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 8 * 60 * 60, 60 * 1000);  // 互联网阿里云时钟服务

// 从互联网同步时间到芯片
bool SetRTCTime() {
  // Variables to save date and time
  String formattedDate;
  String dayStamp;

  bool ret = timeClient.update();
  if (ret) {

    // The formattedDate comes with the following format: 2018-05-28T16:00:13Z
    // We need to extract date and time
    formattedDate = timeClient.getFormattedDate();
    // Serial.println(formattedDate);
    // Extract date
    int splitT = formattedDate.indexOf("T");
    dayStamp = formattedDate.substring(0, splitT);
    l_tmpgetdate[0] = timeClient.getSeconds();
    l_tmpgetdate[1] = timeClient.getMinutes();
    l_tmpgetdate[2] = timeClient.getHours();
    l_tmpgetdate[3] = timeClient.getDay();                // dayOfWeek
    l_tmpgetdate[4] = dayStamp.substring(8, 10).toInt();  // dayOfMonth
    l_tmpgetdate[5] = dayStamp.substring(5, 7).toInt();   // month
    l_tmpgetdate[6] = dayStamp.substring(2, 4).toInt();   // year
    l_tmpgetdate[7] = 0;

    if (l_tmpgetdate[3] == 0)  // 0为周日
    {
      l_tmpgetdate[3] = 7;
    }
  }
  return ret;
}

// 保存参数到EEPROM
void Save_EPROM(void) {
  EEPROM.write(IAP_ADDRESS_STYTE3, (uchar)MyStyte3);
  EEPROM.write(IAP_ADDRESS_WUXIU, WUXIU ? '0' : '1');
  EEPROM.write(IAP_ADDRESS_AUTOLIGHT, AUTOLIGHT ? '0' : '1');
  EEPROM.write(IAP_ADDRESS_LIGHT, LIGHT ? '0' : '1');
  EEPROM.write(IAP_ADDRESS_GREEN, GREEN ? '1' : '0');
  EEPROM.commit();
}

#endif